﻿using CoreShop.Model;
using CoreShop.Services;
using FastMember;
using SqlSugar.Extensions;
using Sunny.UI;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreShop.Library;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.ComponentModel;
using Sunny.UI.Win32;

namespace CoreShop.PrintService
{
    /// <summary>
    /// 驱动打印处理
    /// </summary>
    public class SignalPrintManager
    {
        /// <summary>
        /// 根据订单和用户，取打印方案
        /// </summary> 
        public PrintBillModel GetPrintBillModel(long orderId, int sysUserID,int billTypeId)
        {
            var model = new PrintBillModel();
            var corecmssystemappset = new corecmssystemappsetService().GetById(sysUserID);
            if (corecmssystemappset == null)
            {
                corecmssystemappset = new corecmssystemappset();
                corecmssystemappset.sysUserId = sysUserID;
            }
            model.orderId = orderId;
            switch (billTypeId)
            {
                case (int)GlobalEnumVars.PrintbillType.FoodList:
                    model.printerId = corecmssystemappset.orderingPrtId;
                    break;
                case (int)GlobalEnumVars.PrintbillType.PrePayment:
                    model.printerId = corecmssystemappset.prePayPrtId;
                    break;
                case (int)GlobalEnumVars.PrintbillType.Settlement:
                    model.printerId = corecmssystemappset.settlementPrtId;
                    break;
            }
            return model;
        }

        /// <summary>
        /// 预结算单（汇总打印)
        /// </summary>        
        /// <param name="taskdetails"></param>
        public int SetPrintPrePayment(PrintBillModel regModel,ref string msg)
        {
            var printscheme = new corecmsprintschemeServices().GetById(regModel.printerId);
            if (printscheme == null)
            {
                msg = "打印机不存在，请设置";
                return 1;
            }
            if (printscheme.sendPrintWay != (int)GlobalEnumVars.SendPrintWay.Drive)
            {
                msg = "无法打印，请设置一个驱动打印机。";
                return 1;
            }
            var orderingMaster = new corecmsfoodorderingmasterServices().GetById(regModel.orderId);
            if (orderingMaster == null)
            {
                msg = "订单不存在，请检查";
                return 1;
            }
            var schemedetail = new corecmsprintschemedetailServices().AsQueryable()
                .Where(p => p.printSchemeId == regModel.printerId && p.billtypeId ==
                (int)GlobalEnumVars.PrintbillType.PrePayment).ToList().FirstOrDefault();

            if (schemedetail == null)
            {
                msg = "打印格式未设置，请检查";
                return 1;
            }
            var billtemplate = new corecmsprintbilltemplateServices().GetById(schemedetail.templateId);
            if (billtemplate == null)
            {
                msg = "打印模板未设置，请检查";
                return 1;
            }
            if (string.IsNullOrEmpty(billtemplate.reportFile))
            {
                msg = "打印模板未设置，请检查";
                return 1;
            }
            new ReportServices().SetPrintPrePayment(orderingMaster, billtemplate);
            msg = "打印成功";
            return 0;
        }


        /// <summary>
        /// 客户点菜单（原单打印)
        /// </summary>        
        /// <param name="taskdetails"></param>
        public int SetPrintOrdering(List<corecmsprinttaskdetail> taskdetails
            , corecmsfoodorderingmaster orderMaster,PrintBillModel regModel,ref string msg)
        {
            var printscheme = new corecmsprintschemeServices().GetById(regModel.printerId);
            if (printscheme == null)
            {
                msg = "打印机不存在，请设置";
                return 1;
            }
            if (printscheme.sendPrintWay != (int)GlobalEnumVars.SendPrintWay.Drive)
            {
                msg = "无法打印，请设置一个驱动打印机。";
                return 1;
            }
            var schemedetail = new corecmsprintschemedetailServices().AsQueryable()
                .Where(p => p.printSchemeId == regModel.printerId && p.billtypeId ==
                (int)GlobalEnumVars.PrintbillType.FoodList).ToList().FirstOrDefault();

            if (schemedetail == null)
            {
                msg = "打印格式未设置，请检查";
                return 1;
            }
            var billtemplate = new corecmsprintbilltemplateServices().GetById(schemedetail.templateId);
            if (billtemplate == null)
            {
                msg = "打印模板未设置，请检查";
                return 1;
            }
            if (string.IsNullOrEmpty(billtemplate.reportFile))
            {
                msg = "打印模板未设置，请检查";
                return 1;
            }
            new ReportServices().SetPrinOrdering(taskdetails, billtemplate, orderMaster);
            return 0;
        }

        /// <summary>
        /// 预结算单（汇总打印)
        /// </summary>        
        /// <param name="taskdetails"></param>
        public int SetPrintSettlement(PrintBillModel regModel, ref string msg)
        {
            var printscheme = new corecmsprintschemeServices().GetById(regModel.printerId);
            if (printscheme == null)
            {
                msg = "打印机不存在，请设置";
                return 1;
            }
            if (printscheme.sendPrintWay != (int)GlobalEnumVars.SendPrintWay.Drive)
            {
                msg = "无法打印，请设置一个驱动打印机。";
                return 1;
            }
            var orderMaster = new corecmsfoodordermasterServices().GetById(regModel.orderId);
            if (orderMaster == null)
            {
                msg = "订单不存在，请检查";
                return 1;
            }
            var schemedetail = new corecmsprintschemedetailServices().AsQueryable()
                .Where(p => p.printSchemeId == regModel.printerId && p.billtypeId ==
                (int)GlobalEnumVars.PrintbillType.Settlement).ToList().FirstOrDefault();

            if (schemedetail == null)
            {
                msg = "打印格式未设置，请检查";
                return 1;
            }
            var billtemplate = new corecmsprintbilltemplateServices().GetById(schemedetail.templateId);
            if (billtemplate == null)
            {
                msg = "打印模板未设置，请检查";
                return 1;
            }
            if (string.IsNullOrEmpty(billtemplate.reportFile))
            {
                msg = "打印模板未设置，请检查";
                return 1;
            }
            new ReportServices().SetPrintSettlement(orderMaster, billtemplate);
            msg = "打印成功";
            return 0;
        }


    }
}